<?php

namespace App\Console\Commands;

use App\BookApply;
use App\Document;
use App\PatentApply;
use App\PatentAssess;
use App\PlantVariety;
use App\Transverse;
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Storage;

class LocalFileClearCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:clear:file';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';
    protected $count = 0;
    protected $table = '';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    public function handle()
    {
        $disk = config('admin.upload.disk');
        if ($disk == 'ali_oss') {
            $this->clearM();
        } else {
            echo "【end】$disk--------------- \n";
        }
    }

    protected function clearM()
    {
        echo "【end】 empty--------------- \n";
    }

    protected function clearAll($disk)
    {
        $this->count = 0;
        $this->table = 'BookApply';
        BookApply::query()->select(['file', 'cert'])
            ->orderBy('id')
            ->chunk(100, function ($list) use ($disk) {
                $list->map(function ($model) use ($disk) {
                    $this->clearField($model->toArray());
                });
            });
        echo "【BookApply】delete----------------------- count =$this->count \n ";
        sleep(3);
        $this->count = 0;
        $this->table = 'PatentApply';
        PatentApply::query()->select(['file', 'cert', 'retrieval_report'])
            ->orderBy('id')
            ->chunk(100, function ($list) use ($disk) {
                $list->map(function ($model) use ($disk) {
                    $this->clearField($model->toArray());
                });
            });
        echo "【PatentApply】delete----------------------- count =$this->count \n ";
        sleep(3);
        $this->count = 0;
        $this->table = 'PlantVariety';
        PlantVariety::query()->select(['cert'])
            ->orderBy('id')
            ->chunk(100, function ($list) use ($disk) {
                $list->map(function ($model) use ($disk) {
                    $this->clearField($model->toArray());
                });
            });
        echo "【PlantVariety】delete----------------------- count =$this->count \n ";
        sleep(3);
        $this->count = 0;
        $this->table = 'Transverse';
        Transverse::query()->select(['file', 'file2'])
            ->orderBy('id')
            ->chunk(100, function ($list) use ($disk) {
                $list->map(function ($model) use ($disk) {
                    $this->clearField($model->toArray());
                });
            });
        echo "【Transverse】delete----------------------- count =$this->count \n ";
        sleep(3);
        $this->count = 0;
        $this->table = 'PatentAssess';
        PatentAssess::query()->select(['path'])
            ->orderBy('id')
            ->chunk(100, function ($list) use ($disk) {
                $list->map(function ($model) use ($disk) {
                    $this->clearField($model->toArray());
                });
            });
        echo "【PatentAssess】delete----------------------- count =$this->count \n ";
        sleep(3);
        $this->count = 0;
        $this->table = 'Document';
        Document::query()->select(['path'])
            ->orderBy('id')
            ->chunk(100, function ($list) use ($disk) {
                $list->map(function ($model) use ($disk) {
                    $this->clearField($model->toArray());
                });
            });
        echo "【Document】delete----------------------- count =$this->count \n ";
    }

    protected function clearField($fields)
    {
        $storage = Storage::disk('admin');
        foreach ($fields as $path) {
            if ($path) {
                if (Storage::disk('ali_oss')->exists($path)) {
                    if ($storage->exists($path)) {
                        $storage->delete($path);
                        $this->count++;
                        echo "【 $this->table 】【delete】$path -------------count= $this->count \n";
                    } else {
                        echo "【 $this->table 】【non-existent】$path --- \n";
                    }
                } else {
                    echo "【 $this->table 】【error】$path ---\n";
                }
            }
        }
    }
}
